<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
	
<%@ page import ="com.easyattendance.services.AddTeacherService" %>
<%@ page import ="com.easyattendance.model.Teacher" %>
<%@ page import ="com.easyattendance.services.AddAssignmentService" %>
<%@ page import ="com.easyattendance.services.AddSubjectService" %>
<%@ page import ="com.easyattendance.model.Assignment" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Collections" %>
<%@ page import="java.util.Comparator" %>
	
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Teacher - Assignment Settings</title>
<!-- <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> -->
<jsp:include page="../includeFiles.jsp" />

<!-- fooTable Links -->
<link href="../css/footable.core.css" rel="stylesheet" type="text/css" />
<link href="../css/footable.metro.css" rel="stylesheet" type="text/css" />
<script src="<%=request.getContextPath()%>/public/js/footable.js" type="text/javascript"></script>
<script src="<%=request.getContextPath()%>/public/js/footable.sort.js" type="text/javascript"></script>
<script type="text/javascript">
	$(function () {
		$('.footable').footable();
	});
</script>
</head>
	
<% String currentUser = (String) session.getAttribute("currentUser");
   AddSubjectService subjectService=new AddSubjectService();
   
    AddAssignmentService addAssignmentService = new AddAssignmentService();	
    List<Assignment> AssignmentList = new ArrayList<Assignment>();
	AssignmentList = addAssignmentService.listAssignments(currentUser);
	/* Sorting list based on Class id */
	Collections.sort(AssignmentList,new Assignment.SortAssignmentByClass());
	
	Assignment tempAssignment = null;
	int i=0;					
	Iterator AssignmentItr = AssignmentList.iterator();
	AddTeacherService addTeacherService = new AddTeacherService();
	Teacher teacher_role = addTeacherService.getTeacherByEmailId(currentUser);
   
%>

<body class="left-sidebar">

<!-- Session check : user logged in or not/ press back button -->
<jsp:include page="../common/checkSession.jsp" />
<jsp:include page="../common/checkTeacherRole.jsp" />

	<!-- Wrapper -->
	<div id="wrapper">

		<!-- Content -->
		<div id="content">
			<div id="content-inner">

				<div>
					<form action="<%=request.getContextPath()%>/views/teacher/addAssignment.jsp">
					<% if(teacher_role.isClassTeacher()){ %>
						<input type="submit" value="Add Assignment" class="button add_button" tabindex="1">
					<% }else{ %>
						<input type="submit" value="Add Assignment" class="add_button" disabled="disabled">
					<% } %>
					</form>
				</div>	
				
				<span class="success_msg">${assignmentDeleted}</span>
				<span class="error_msg">${assignmentNotDeleted}</span>
				<span class="error_msg">${invalidChoice}</span>
				
		<div >
			<form name="assignment_form" id="assignment_form" action="${pageContext.request.contextPath}/AddAssignmentServlet" class="gridview">
				<table name="assignment" id="tbl_attendance" class="footable metro-attendance">
                <thead>
                <tr>
                 	<th data-sort-ignore="true"></th>
                    
                    <th data-toggle="true" data-sort-ignore="true">
                        Assignment Id
                    </th> 
                   
                    <th data-toggle="true" data-sort-ignore="true">
                        Class Id
                    </th>
                    <th data-toggle="true" data-sort-ignore="true">
                        Assignment Name
                    </th>
                    
                    <th data-toggle="true" data-sort-ignore="true">
                        Subject Name
                    </th>
                   <!--   <th data-toggle="true" data-sort-ignore="true">
                        Path
                    </th> -->
                   
                </tr>
                </thead>
              <tbody>
		
		
			<% while(AssignmentItr.hasNext()) { i++;%>
						 <% try {
									tempAssignment = (Assignment) AssignmentItr.next();
								} 
								catch(Exception ex){
									ex.printStackTrace();
								}%> 
								
				<tr>				
				    <td>
						<input type="checkbox" id="checkbox" name="checkbox<%=i%>" class="chk" value="<%=(i-1)%>">
					</td>
					
					 <td data-value="<%= tempAssignment.getAssignment_id() %>">
						<input type="text" name="assignment_id" value="<%= tempAssignment.getAssignment_id() %>" id="assignment_id" readonly="readonly"/>
					</td> 
					
					<td data-value="<%=tempAssignment.getClass_id() %>">
						 <input type="text" id="class_id" name="class_id" value="<%=tempAssignment.getClass_id()%> " readonly="readonly" />	 
					</td>
					
					<td data-value="<%=tempAssignment.getAssignment_name() %>">
						<input type="text" id="assignment_name" name="assignment_name" value="<%=tempAssignment.getAssignment_name()%>" readonly="readonly" />
					</td>
					
					<td data-value="<%=subjectService.getSubjectBySubjectId(tempAssignment.getSubject_id()).getSubject_name() %>">
						<input type="text" id="subject_name" name="subject_name" value="<%=subjectService.getSubjectBySubjectId(tempAssignment.getSubject_id()).getSubject_name() %>" readonly="readonly" />		
					</td>
					
					<!--  <td data-value="<%-- <%=tempAssignment.getPhysical_path() %> --%>">
						<input type="text" id="path" name="path" value="<%--<%=tempAssignment.getPhysical_path()%> --%>" />	
					</td> -->
			<% } %>
				
	 </tbody>
   </table>
   
   <input type="hidden" id="action_type" name="action_type" value="delete" /> 
   <% if(teacher_role.isClassTeacher()){ %>
   		<input type="submit" id="deleteAssignment" value="Delete Selected Assignments" class="button add_button" onclick="return confirm('Are you sure you want to delete?')">
   <% }else{ %>
   		<input type="submit" id="deleteAssignment" value="Delete Selected Assignments" class="add_button" disabled="disabled">
   <% } %>
   
				
 </form>
</div>						
					
	</div>
</div>					
					

		<!-- Sidebar -->
		<div id="sidebar">

			<!-- Sidebar Header - Logo & Name -->
			<jsp:include page="../common/sidebarHeader.jsp" />

			<jsp:include page="teacherMenu.jsp" />

			<!-- Sidebar Footer Content -->
			<jsp:include page="../common/sidebarFooter.jsp" />

		</div>

	</div>	
	<script type="text/javascript">
	 $("#deleteAssignment").click(function() {
	         getSelectedAssignments();
	    });
	 
	 function getSelectedAssignments(){
		    /* declare an checkbox array */
		    var chkArray = [];
		     
		    /* look for all checkboes that have a class 'chk' attached to it and check if it was checked */
		    $(".chk:checked").each(function() {
		        chkArray.push($(this).val());
		    });
		     
		    /* we join the array separated by the comma */
		    var selected;
		    selected = chkArray.join(',') + ",";
		     
		    /* check if there is selected checkboxes, by default the length is 1 as it contains one single comma */
		    if(selected.length==1){
		    
		        alert("Please select at least one of the assignments to delete"); 
		        $("#assignment_form").attr("action", ""); 
		    }
		    else if(selected>1)
		    {
		    	 getSelectedCheckboxes();
		    }  
		}
	 
	 function getSelectedCheckboxes(checkbox)
	 {
	    /* var options = checkbox.getElementsByTagName('input');
	     //var arrayOfCheckBoxLabels= checkBoxList.getElementsByTagName("label");
	     for(i=0;i<=options.length;i++)
	     {
	         var opt = options[i];
	         var indexes="";
	         if(opt.checked)
	         {
	        	 indexes=indexes+i;
	             alert(indexes); 
	         } 
	     } */
	 }
	
	</script>
	
	<script type="text/javascript">
		$(function(){
			$("#assignmentSettings").addClass("current_page_item");
		});
	</script>

</body>
</html>